package com.markedquilt.service.impl;
/*
 * PackageName: com.markedquilt.service.impl
 * ClassName:   ArticleServiceImpl
 * Author:   zhang
 * Date:     Created in 2020/11/2
 * Description:
 * History:
 * <author>        <time>             <version>          <desc>
 * 作者姓名         修改时间            版本号              描述
 * zhang           2020/11/2      1.0
 */

import com.markedquilt.dao.ArticleDao;
import com.markedquilt.service.ArticleService;
import com.markedquilt.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Title:
 * Project: springbootTask
 *
 * @author ZhangKe
 * Created on 2020/11/2
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Override
    public ArticleVo findById(Integer id) {
        ArticleVo articleVo = new ArticleVo();
        articleVo.getArticles().add(articleDao.findById(id));
        return articleVo;
    }

    @Override
    public ArticleVo findAll() {
        ArticleVo articleVo = new ArticleVo();
        articleVo.getArticles().addAll(articleDao.findAll());
        //设置包装内内部属性
        articleVo.setNumPage(1);
        articleVo.setCurrentPage(1);
        articleVo.setRows(articleDao.countArticle());
        return articleVo;
    }

    @Override
    public ArticleVo findPagedAll(int page,int rows) {
        ArticleVo articleVo = new ArticleVo();
        //总页数
        int totalPage = (articleDao.countArticle())%rows == 0? (articleDao.countArticle())/rows : (articleDao.countArticle())/rows+1;
        if (page<=0) {
            page=1;
        }else if (page>=totalPage){
            page=totalPage;
        }
        articleVo.getArticles().addAll(articleDao.findPagedAll(page-1,rows));
        //设置包装内内部属性
        articleVo.setNumPage(totalPage);
        articleVo.setCurrentPage(page);
        articleVo.setRows(rows);
        return articleVo;
    }
}
